package com.szgr.baoyin.activity;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.szgr.baoyin.MyApplication;
import com.szgr.baoyin.R;
import com.szgr.baoyin.adapter.MyOrderListAdapter;
import com.szgr.baoyin.adapter.RefundListAdapter;
import com.szgr.baoyin.model.OrderInfo;
import com.szgr.baoyin.model.Refund;
import com.szgr.baoyin.utils.Constant;
import com.szgr.baoyin.utils.RLog;
import com.szgr.baoyin.utils.ViewFindUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ReturnGoodsActivity extends BaseActivity {

    private List<Refund> refunds;
    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case 200:
                    listAdapter.setData(refunds);
                    break;
            }
        }
    };
    private RefundListAdapter listAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_return_goods);
        super.onCreate(savedInstanceState);
    }

    @Override
    public void initActionBar() {
        actionBarTitle.setText("退货");
    }

    @Override
    public void initView() {
        ListView listView = ViewFindUtils.find(view,R.id.list_view);
        refunds = new ArrayList<>();
        listAdapter = new RefundListAdapter(this, refunds, this);
        listView.setAdapter(listAdapter);

        getReturnGoods();
    }

    private void getReturnGoods() {
        StringRequest sr = new StringRequest(Request.Method.POST, Constant.URL_TUIHUO,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String s) {
                        RLog.s("---result--->" + s);
                        try {
                            JSONObject jsonObject = new JSONObject(s);
                            JSONObject datas = jsonObject.getJSONObject("datas");
                            JSONArray order_refund_list = datas.getJSONArray("order_refund_list");
                            refunds = new Gson().fromJson(order_refund_list.toString(), new TypeToken<List<Refund>>() {
                            }.getType());
                            mHandler.obtainMessage(200).sendToTarget();
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {

            }
        }){
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String,String> map = new HashMap<>();
                map.put("key",MyApplication.getmInstance().getKey());
                return map;
            }
        };
        MyApplication.getmInstance().addToRequestQueue(sr);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.action_bar_left:
                finish();
                break;
        }
    }
}
